<template>
  <div>
    <div>
      <el-row style="margin-bottom:10px">
        <el-col :span="8" >
          <h4 style="margin:0"> 
            <span>搜索管理</span>
            <el-button  type="success" class="addBtn" v-if="userRole.KW_301001" @click="addFun"><i class="el-icon-plus"></i>关键字</el-button>
          </h4>
        </el-col>
        <el-col :span="16" style="text-align:right">
          <el-input v-model="sheachValue" style="width:300px" placeholder="输入搜索内容"></el-input>
          <el-button type="primary" @click="seachFun">搜索</el-button>
        </el-col>
      </el-row>
      
      <el-table :data="tableData" border style="width: 100%" >
        <el-table-column prop="name" label="序号" width="60">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        
        <el-table-column prop="keyword" label="关键字"> </el-table-column>      
        <el-table-column prop="page_view" label="点击量">> </el-table-column>      
        
        <el-table-column label="操作">
          <template slot-scope="scope" >
            <div>
              <el-button class="addBtn" @click="editFun(scope.row)" v-if="userRole.KW_301002"> 编辑</el-button>
              <el-button type="danger"  class="addBtn" @click="deletData(scope.row.id)" v-if="userRole.KW_301003"> 删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2"  class="demo-ruleForm">
          
          <el-form-item label="" prop="keyword">
            <el-input v-model.number="ruleForm2.keyword"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button  @click="handleClose" style="margin-right:80px">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm2')">确 定</el-button>
        </span>
      </el-dialog>
     
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import Http from '@/Http/Http'

  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入关键字'));
        } else {
          callback();
        }
      };
      return {
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        currentPage4: 1,//分野  
        total:0,      
        dialogVisible:false,
        tableData: [],
        page:1,
        editId:'',
        title:'添加关键字',
        pageSize:10,
        sheachValue:'',
        ruleForm2: {
          keyword: ''
        },
        rules2: {
          keyword: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getlist()
    },
    methods: {
       getlist() {
        
        const parm ={
          "pageIndex":this.page,
          "pageSize":this.pageSize,
          "keyword":this.sheachValue,
        };
        
        Http.getKeywordlList(parm)
        .then( res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
           this.dialogVisible = false;
          
        })
        .catch( error => {
          
        })
      },  
      addFun(){
        this.dialogVisible = true;
        this.title = '添加关键字';
      },
      editFun(val) {
        this.title = '修改关键字';
        this.editId = val.id;
        this.ruleForm2.keyword = val.keyword;
        this.dialogVisible = true;
      },
      deletData(row) {
        
        this.$confirm(`确认删除此关键字吗？`)
        .then(_ => {
          Http.deleteKeyword(row)
          .then( res => {
            
            if( res.data.code == 200 ){
              this.$message.success('删除成功')
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}`)
              this.$router.push('/')
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          
        });
      },
      
      handleClose(done) {
        this.ruleForm2.keyword ='';
        this.dialogVisible = false
       this.$refs['ruleForm2'].resetFields();
      },
      
       //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
     
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.editId ? this.editKeywordFun():this.addKeywordFun();
             this.editId ='';
             this.ruleForm2.keyword ='';
          } else {
            return false;
          }
        });
      },
      addKeywordFun(){
       
        Http.addKeyword({"keyword":this.ruleForm2.keyword})
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('关键字添加成功')
            
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字添加失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      editKeywordFun(){
        Http.editKeyword({"keyword":this.ruleForm2.keyword,'id':this.editId})
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('关键字修改成功')
            
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字修改失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      seachFun(){
        this.getlist()
      }
    }
  }
</script>